/**
 * @brief ISockHandler is a Multiplexor callback interface that handles\n
 *        reading & sending io events.
 *
 * @details Application implements ISockHandler and registers to\n
 *         Multiplexor.\n 
 *         When received data, Multiplexor will call ISockHandler::Receive();\n
 *         The stack sending buffer isn't full, call ISockHandler::Send().\n
 *         Occurred error, call ISockHandler::Error().
 *
 * @author Huang RuiGuang
 * @date 2008/12/1
 *
 * Herm library
 * Copyright (c) 2008 by Huang RuiGuang(daemon.huang@gmail.com)
 *
 * Permission to use, copy, modify, distribute and sell this software.
 */


#ifndef HERM_SOCKET_SOCKET_HANDLER_H
#define HERM_SOCKET_SOCKET_HANDLER_H

#include "herm/export_symbols.h"
#include "herm/base/socket/socket_def.h"

namespace herm{

// Masks
const MultiplexorMask READ_MASK = Private::COMMON_READ_MASK;    ///< Read IO event
const MultiplexorMask WRITE_MASK = Private::COMMON_WRITE_MASK;  ///< Write IO event
const MultiplexorMask ALL_EVENTS_MASK = READ_MASK | WRITE_MASK; ///< Read & write

struct HERM_API ISockHandler
{
	virtual ~ISockHandler(){}

	/**
     * @brief Get socket handle.
	 * @note Application must implements GetHandle() and returns socket\n
	 *       handle.
	 * @return A valid socket handle.
	 * @remarks Multiplexor will be call this method when register a handler to
	 *          handle manager.
	 */
	virtual Handle OnGetHandle() const = 0;

	/**
     * @brief Be called when received data.
	 * @param [availBytes] Only support FreeBSD kqueue multiplexor. Application can\n 
	 *        read \a availBytes bytes.
	 * @return true, successfully; false, handle failed, and call Error().
	 */
	virtual int OnReceive(int availBytes = 0) = 0;

	/**
     * @brief Be called when sending buffer can write.
	 * @param [availBytes] Only support FreeBSD kqueue multiplexor. Application can\n 
	 *        write \a availBytes bytes.
	 * @return true, successfully; false, handle failed, and call Error().
	 */
	virtual int OnSend(int availBytes = 0) = 0;

	/**
     * @brief Sending and Receiving occurred error(return -1), Error() will be called.
	 * @note Application don't need to unregister(Multiplexor::Remove) and close socket fd.
	 *       Library would automatically unregister and close.
	 * @param [state] state.
	 */
	virtual void OnError(int stateType, int stateCode) = 0;
};
}

#endif 
